import React from 'react';
import { Switch, Route, Redirect } from 'react-router-dom'
const RouterView = ({ routes }) => {
    const comList = routes.filter(item => item.com);
    const redList = routes.filter(item => item.to)
    return (
        <Switch>
            {comList.map(item => {
                if (item.needLogin) {
                    // 判断登录状态
                    const isLogin = window.sessionStorage.getItem('user')
                    if (!isLogin) {
                        // 未登录则重定向到登录页
                        return <Redirect key={item.path} to="/login"></Redirect>
                    }
                }
                return <Route key={item.path} path={item.path} render={(history) => {
                    return <item.com {...history} routes={item.children || []}></item.com>
                }}></Route>
            })}
            {redList.map(item => {
                return <Redirect key={item.to} {...item}></Redirect>
            })}
        </Switch>
    );
}

export default RouterView;
